package com.situ.mall.common.service.impl;

import com.situ.mall.common.entity.Comment;
import com.situ.mall.common.mapper.CommentMapper;
import com.situ.mall.common.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.common.vo.CommentVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JavaWizard
 * @since 2024-04-18
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    // 查询全部评论
    @Override
    public ArrayList<CommentVO> selectAll(Integer id) {
        // 1.先查一级评论
        ArrayList<CommentVO> commentVOS = baseMapper.selectFirst(id);

        // 2.如果一级评论不为空，遍历一级评论，查它的二级评论，放进去
        if (commentVOS != null) {
            for (CommentVO commentVO : commentVOS) {
                ArrayList<CommentVO> secondComment = baseMapper.selectSecond(commentVO.getId());
                commentVO.setCommentVOS(secondComment);

                // 3.如果二级评论不为空，遍历二级评论，查它的三级评论，放进去
                if (secondComment != null) {
                    for (CommentVO vo : secondComment) {
                        ArrayList<CommentVO> thirdComment = baseMapper.selectThird(vo.getId());
                        vo.setCommentVOS(thirdComment);
                    }
                }
            }
        }


        // 4.返回
        return commentVOS;
    }
}
